
var uzStorage = function() {
  var ls = window.sessionStorage
  return ls
}
// 定义全局变量us
const us = {}
// 设置缓存
us.setStorage = function(key, value) {
  var v = value
  if (typeof v === 'object') {
    v = JSON.stringify(v)
    v = 'obj-' + v
  } else {
    v = 'str-' + v
  }
  var ls = uzStorage()
  if (ls) {
    ls.setItem(key, v)
  }
}
// 获取缓存
us.getStorage = function(key) {
  var ls = uzStorage()
  if (ls) {
    var v = ls.getItem(key)
    if (!v) {
      return false
    }
    if (v.indexOf('obj-') === 0) {
      v = v.slice(4)
      return JSON.parse(v)
    } else if (v.indexOf('str-') === 0) {
      return v.slice(4)
    }
  }
}

// 移除缓存
us.removeStorage = (key) => {
  if (!key) return false
  var ls = uzStorage()
  ls.removeItem(key)
}

export default us
